﻿using System;
using System.Diagnostics;
using System.IO;
using UnityEditor;
using UnityEngine;

namespace XUCompress
{
    namespace Editor
    {
        internal class XUCompressWindow : EditorWindow
        {
            [MenuItem("XUPK/XUCompress/Tests", false, 1)]
            public static void SetConfig()
            {
                EditorWindow window = GetWindow<XUCompressWindow>("Compress");
                window.minSize = window.maxSize = new Vector2(812, 600);
                window.Show();
            }

            private string filePath;

            private TimeSpan spanZipCompress;
            private TimeSpan spanZipDecompress;
            private TimeSpan spanLzmaCompress;
            private TimeSpan spanLzmaDecompress;

            private long sizeFile;
            private long sizeZip;
            private long sizeLzma;

            private void OnGUI()
            {
                GUILayout.Space(10);

                filePath = GUILayout.TextField(filePath);

                GUILayout.BeginHorizontal();
                GUILayout.FlexibleSpace();
                if (GUILayout.Button("选择文件"))
                {
                    filePath = EditorUtility.OpenFilePanel("文件", Application.dataPath, "*.*");
                    sizeFile = new FileInfo(filePath).Length;
                }
                GUILayout.EndHorizontal();

                GUILayout.Space(10);

                GUILayout.BeginHorizontal();
                {
                    GUILayout.BeginVertical("box", GUILayout.Width(400), GUILayout.Height(200));
                    {
                        if (GUILayout.Button("Zip测试"))
                        {
                            if (!string.IsNullOrEmpty(filePath))
                            {
                                string pathCompress = filePath + ".zip.compress";
                                string pathDecompress = filePath + ".zip.decompress";
                                {
                                    Stopwatch sw = new Stopwatch();
                                    sw.Start();
                                    XUCompressZipLib.Zip(new string[] { filePath }, pathCompress);
                                    sw.Stop();
                                    spanZipCompress = sw.Elapsed;
                                }
                                {
                                    Stopwatch sw = new Stopwatch();
                                    sw.Start();
                                    XUCompressZipLib.UnzipFile(pathCompress, pathDecompress);
                                    sw.Stop();
                                    spanZipDecompress = sw.Elapsed;
                                }
                                sizeZip = new FileInfo(pathCompress).Length;

                                AssetDatabase.Refresh();
                            }
                        }
                        if (sizeFile > 0 && sizeZip > 0)
                        {
                            GUILayout.Label("源文件大小：" + GetSizeString(sizeFile));
                            GUILayout.Label("压缩后大小：" + GetSizeString(sizeZip));
                            GUILayout.Label("压缩比例：" + Mathf.FloorToInt(sizeZip * 1f / sizeFile * 100) + "%");
                            GUILayout.Label("压缩时间：" + spanZipCompress.TotalMilliseconds + "ms");
                            GUILayout.Label("解压时间：" + spanZipDecompress.TotalMilliseconds + "ms");
                        }
                    }
                    GUILayout.EndVertical();

                    GUILayout.BeginVertical("box", GUILayout.Width(400), GUILayout.Height(200));
                    {
                        if (GUILayout.Button("LZMA测试"))
                        {
                            if (!string.IsNullOrEmpty(filePath))
                            {
                                string pathCompress = filePath + ".lzma.compress";
                                string pathDecompress = filePath + ".lzma.decompress";
                                {
                                    byte[] bytes = File.ReadAllBytes(filePath);
                                    Stopwatch sw = new Stopwatch();
                                    sw.Start();
                                    XUCompress7zip.CompressLZMA(bytes, pathCompress);
                                    sw.Stop();
                                    spanLzmaCompress = sw.Elapsed;
                                }
                                {
                                    byte[] bytes = File.ReadAllBytes(pathCompress);
                                    Stopwatch sw = new Stopwatch();
                                    sw.Start();
                                    XUCompress7zip.DecompressLZMA(bytes, pathDecompress);
                                    sw.Stop();
                                    spanLzmaDecompress = sw.Elapsed;
                                }
                                sizeLzma = new FileInfo(pathCompress).Length;

                                AssetDatabase.Refresh();
                            }
                        }
                        if (sizeFile > 0 && sizeLzma > 0)
                        {
                            GUILayout.Label("源文件大小：" + GetSizeString(sizeFile));
                            GUILayout.Label("压缩后大小：" + GetSizeString(sizeLzma));
                            GUILayout.Label("压缩比例：" + Mathf.FloorToInt(sizeLzma * 1f / sizeFile * 100) + "%");
                            GUILayout.Label("压缩时间：" + spanLzmaCompress.TotalMilliseconds + "ms");
                            GUILayout.Label("解压时间：" + spanLzmaDecompress.TotalMilliseconds + "ms");
                        }
                    }
                    GUILayout.EndVertical();
                }
                GUILayout.EndVertical();

                GUILayout.BeginHorizontal();
                {
                    GUILayout.BeginVertical("box", GUILayout.Width(400), GUILayout.Height(200));
                    {

                    }
                    GUILayout.EndVertical();
                }
                GUILayout.EndHorizontal();
            }

            public static string GetSizeString(long fileSize)
            {
                if (fileSize >= (1024 * 1024))
                {
                    return string.Format("{0:F2}MB", fileSize * 1f / (1024 * 1024));
                }
                else
                {
                    return string.Format("{0:F2}KB", fileSize * 1f / 1024);
                }
            }
        }
    }
}